using System;
using System.Collections.Generic;
using System.Drawing;

namespace MDAControlsFramework.Helpers {
	/// <summary>
	/// Trida schranujici text a jeho velikost
	/// </summary>
	public class MDAText {
		private string _text = "";

		public static MDAText None;

		/// <summary>
		/// spocitana velikost
		/// </summary>
		private Size _size = Size.Empty;

		public MDAText(string text) {
			_text = text;
		}

		public MDAText() {
			_text = "";
		}

		public MDAText DeepCopy() {
			MDAText text = new MDAText(_text);
			return text;
		}

		#region property

		public Size Size {
			get {
				return _size;
			}
		}

		public string Text {
			get {
				return _text;
			}
			set {
				_text = value;
			}
		}

		#endregion

		// z puvodniho grameworku
		//protected virtual void DrawTextLine(Graphics gr, string line, Font font, Brush brush, int x, int y, int width, int height, int textIndex) {
		//    StringFormat sf = new StringFormat();
		//    sf.Alignment = StringAlignment.Center;
		//    sf.LineAlignment = StringAlignment.Far;
		//    StringFormatFlags.NoClip;

		//    Graphics.MeasureString(s, font);

		//    gr.DrawString(line, font, brush, (float)x, (float)y);
		//}

		public void Draw(Graphics gr, MDATextStyle textStyle, Rectangle DrawRectangle) {
			gr.DrawString(_text, textStyle.Font, textStyle.Brush, DrawRectangle, textStyle.Format);
		}

		/// <summary>
		/// zmeri velikost textu
		/// </summary>
		/// <param name="gr">grafika, pod kterou se to deje</param>
		/// <param name="textStyle">styl textu</param>
		/// <param name="width">maximalni sirka textu</param>
		internal void MeasureString(Graphics gr, MDATextStyle textStyle, int width) {
			List<string> radky = new List<string>();
			int maxWidth = 0;
			int maxHeight = 0;

			if (textStyle == null) {
				return;
			}

			SizeF ef; // = gr.MeasureString(_text, textStyle.Font );
			//float sirka = ef.Width;
			// muj kod
			string[] strRadky = _text.Split('\n');
			foreach (string radek in strRadky) {
				ef = gr.MeasureString(radek.Trim(), textStyle.Font);
				if ((int)ef.Width < width || !textStyle.WordWrap) {
					// radek se vejde 
					maxHeight += (int)ef.Height;
					maxWidth = Math.Max(maxWidth, (int)ef.Width);
					radky.Add(radek.Trim());
				} else {
					int firstChar = 0;
					int lastChar = radek.Length;
					do {
						// najdi maximalni delku retezce
						while (gr.MeasureString(radek.Substring(firstChar, lastChar), textStyle.Font).Width > width) {
							lastChar--;
						}

						// kdyz neni presne zarovnan
						if (char.IsWhiteSpace(radek[lastChar])) {
							lastChar--;
						} else {
							// najdi posledni prazdny znak
							int newLastChar = _text.LastIndexOfAny(new char[] { ' ', '\t' }, firstChar, lastChar);
							if (newLastChar > 0) {
								// pokud tam neni prazdny znak, tak ten retezec rozdelim
								lastChar = newLastChar;
							}
						}

						//tento retezec zpracuj
						ef = gr.MeasureString(radek.Substring(firstChar, lastChar).Trim(), textStyle.Font);
						maxHeight += (int)ef.Height;
						maxWidth = Math.Max(maxWidth, (int)ef.Width);
						radky.Add(radek.Substring(firstChar, lastChar).Trim());

						// pokracuj, dokud nezpracujes cele
					} while (lastChar < radek.Length);
				}
			}

			_size.Width = maxWidth;
			_size.Height = maxHeight;
		}

		public override string ToString() {
			return "Text:\"" + _text + "\"";
		}
	}
}

